home *** CD-ROM | disk | FTP | other *** search
- // MacDemo.c
- // © Copyright 1984, 1987 Consulair Corporation, All Rights Reserved */
- /*
- This is a general Mac demonstration program which shows windows,
- menus, events, and textedit. Windows can be grown, moved, and closed.
- It demonstrates the flavor of MacC Jr. in the Macintosh environment.
-
- If you allow the flag UseWithStdLib to be defined, MacDemo uses the TTY
- window If you don't define it, save the file as SmallDemo.c and Compile
- and Go. The resulting smaller version doesn't print a TTY message out.
- */
-
- #Options Z
-
- #define UseWithStdLib
-
-
- /* Declarations */
-
- #include <MacCDefs.h>
- #ifdef UseWithStdLib
- #include <Stdio.h>
- #else
- static strlen(s) register char *s;{register int i=0; while (s[i])i++; return(i);}
- #endif
- #include <Window.h>
- #include <Events.h>
- #include <TextEdit.h>
- #include <Menu.h>
-
- /* Declared Here */
-
- MenuHandle DeskMenu;
- MenuHandle EditMenu;
- MenuHandle Menu;
-
- #define Desk_ID 200
- #define Edit_ID 201
- #define Menu_ID 202
-
- Rect screenRect = {0, 0, 384, 512};
- Rect windowRectA = {50, 50, 200, 400};
- Rect windowRectB = {60, 60, 210, 410};
-
- WindowPtr openWindow();
-
- #define False 0
- #define True 0xFF
-
- /* Declared Elsewhere */
-
- #ifdef UseWithStdLib
- extern WindowPtr console; /* Std Lib TTY Window */
- #endif
-
- /* Code */
-
- Init()
- {
- InitFonts();
- InitDialogs(0);
- TEInit();
- InitMenus();
- InitCursor();
-
- /* Desk Accessory menu */
- DeskMenu = NewMenu(Desk_ID,"\p\024");
- AddResMenu(DeskMenu, 'DRVR');
- InsertMenu(DeskMenu, 0);
-
- /* Edit menu */
- EditMenu = NewMenu(Edit_ID, "\pEdit");
- AppendMenu(EditMenu,
- "\pUndo;(-;Cut/X;Copy/C;Paste/V;Clear");
- InsertMenu(EditMenu, 0);
- DisableItem(EditMenu, 0);
-
- /* "Menu" menu */
- Menu = NewMenu(Menu_ID, "\pMenu");
- AppendMenu(Menu,
- "\pItem 1;(Dimmed Item 2;Item 3;(-;Item 5/5;Quit/.");
- InsertMenu(Menu, 0);
-
- DrawMenuBar();
- }
-
-
- main()
- {
- char c, *str;
- short windowcode;
- long menuResult;
-
- EventRecord event;
- TEHandle hTE;
- WindowPtr mouseWindow, window, windowA, windowB;
-
- if (CatchSignal()) ExitToShell();
-
- Init();
-
- #ifdef UseWithStdLib
-
- printf("\r\r Mac C Demo\rCopyright Consulair Corporation 1987\r All Rights Reserved\r\r");
- printf("\rThis is a simple demonstration program with two windows.\r");
- printf("\rWhen you strike a key, this TTY window will be erased, and");
- printf("\rTwo windows will be displayed. Typed text will go into the front");
- printf("\rone, and you can switch between the two with a mouse click.");
- printf("\rSelect Quit from the menu or hit a command period to stop.");
-
- getchar();
- DisposeWindow(console);
-
- #endif
-
- windowB =
- openWindow(&windowRectB, "\pDemo Window B",
- "\r Mac C Demo\r\251 Consulair Corporation 1987\r All Rights Reserved\r\rThis is Window B");
-
- windowA =
- openWindow(&windowRectA, "\pDemo Window A",
- "\r Mac C Demo\r\251 Consulair Corporation 1987\r All Rights Reserved\r\rThis is Window A");
-
- hTE = 0;
- InitCursor();
- FlushEvents(0xFFFF);
- SelectWindow(windowA); /* Generate an activate event for window A */
-
- while (True)
- {
- SystemTask();
- if (hTE) TEIdle(hTE);
-
- if (GetNextEvent(everyEvent, &event))
- {
- switch ( event.what )
- {
- case autoKey:
- case keyDown:
- {
- c = event.message;
- if ((event.modifiers & cmdKey))
- DoMenu(MenuKey(c));
- else
- if (hTE) TEKey(c, hTE);
- break;
- }
-
- case mouseDown:
- {
- windowcode = FindWindow(&event.where, &mouseWindow);
-
- if (FrontWindow() != mouseWindow)
- if (mouseWindow != 0)
- {
- SelectWindow(mouseWindow);
- break;
- }
-
- if ((window != 0) && (window == mouseWindow))
-
- {
- if (mouseWindow != 0)
- {
- SetPort(mouseWindow);
- switch ( windowcode )
- {
- case inContent:
- {
- GlobalToLocal(&event.where);
- TEClick(&event.where,
- (event.modifiers & shiftKey)? True:False, hTE);
- break;
- }
- case inDrag:
- {
- DragWindow(mouseWindow, &event.where, &screenRect);
- break;
- }
- case inGrow:
- {
- long growResult;
- short vert, horiz;
- growResult =
- GrowWindow(mouseWindow, &event.where, &screenRect);
- horiz = growResult;
- vert = HiWord(growResult);
- SizeWindow(mouseWindow, horiz, vert, True);
- EraseRect(&mouseWindow->portRect);
- InvalRect(&mouseWindow->portRect);
- SizeTE(mouseWindow);
- DrawGrowIcon(mouseWindow);
- break;
- }
- case inGoAway:
- {
- if (TrackGoAway(window, &event.where))
- {
- TEDispose(hTE);
- hTE = 0;
- DisposeWindow(window);
- window = 0;
- }
- break;
- }
- }
- }
- }
- else
- {
- switch ( windowcode )
- {
- case inMenuBar:
- {
-
- DoMenu(MenuSelect(&event.where));
- break;
- }
- case inSysWindow:
- {
- SystemClick(&event, mouseWindow);
- break;
- }
- case inDrag:
- {
- DragWindow(mouseWindow, &event.where, &screenRect);
- break;
- }
- case inGoAway:
- {
- break;
- }
- }
- }
-
- break;
- }
-
- case updateEvt:
- {
- TEHandle temp_hTE;
- WindowPtr tempWindow;
-
- SetPort(tempWindow = (WindowPtr)event.message);
- BeginUpdate(tempWindow);
- temp_hTE = (TEHandle)GetWRefCon(tempWindow);
- TEUpdate(&tempWindow->portRect, temp_hTE);
- DrawGrowIcon(tempWindow);
- EndUpdate(tempWindow);
- break;
- }
-
- case activateEvt:
- {
- TEHandle temp_hTE;
- WindowPtr tempWindow;
-
- SetPort(tempWindow = (WindowPtr)event.message);
- temp_hTE = (TEHandle)GetWRefCon(tempWindow);
- if ((event.modifiers & activeFlag))
-
- {
- window = tempWindow;
- TEActivate(hTE = temp_hTE)
- }
- else TEDeactivate(temp_hTE);
- DrawGrowIcon(tempWindow);
- break;
- }
- }
- }
-
- }
- }
-
- DoMenu(menuresult)
- long menuresult;
- {
- short menuID, itemNumber;
-
- menuID = HiWord(menuresult);
- itemNumber = menuresult;
-
- switch ( menuID )
- {
- case Menu_ID:
- {
- switch ( itemNumber )
- {
- case 1: break;
- case 2: break;
- case 3: break;
- case 5: break;
- case 6:
- Signal("All Done");
- }
- break;
- }
- case Desk_ID:
- {
- struct P_Str AccessoryName;
- GetItem(DeskMenu, itemNumber, &AccessoryName);
- OpenDeskAcc(&AccessoryName);
- EnableItem(EditMenu, 0);
- DrawMenuBar();
- break;
- }
- case Edit_ID:
- {
- SystemEdit(itemNumber-1);
- break;
- }
- }
- HiliteMenu(0);
- }
-
-
- Rect *TERect(window, rect)
- WindowPtr window;
- Rect *rect;
- {
- BlockMove(&window->portRect, rect, sizeof(Rect));
- rect->right -= 16; /* Make room for scroll bar */
- rect->bottom -= 16; /* Make room for scroll bar */
- }
-
- TEHandle openTE(window)
- WindowPtr window;
- {
- Rect destRect, viewRect;
- TEHandle hTE;
-
- TERect(window, &viewRect);
- TERect(window, &destRect);
- destRect.left += 4; /* indent a bit */
- return TENew(&destRect, &viewRect);
- }
-
- WindowPtr openWindow(rect, title, str)
- Rect *rect;
- char *title, *str;
- {
- WindowPtr window;
- TEHandle hTE;
- window = NewWindow(0, rect, title, True, 0, -1, True, 0);
- SetPort(window);
- hTE = openTE(window);
- SetWRefCon(window, hTE);
- TESetText(str, strlen(str), hTE);
- TEUpdate(&(*hTE)->viewRect, hTE);
- return window;
- }
-
- sizeTE(window)
- WindowPtr window;
- {
- Rect rect;
- TEHandle hTE;
-
- hTE = (TEHandle)GetWRefCon(window);
- TERect(window, &rect);
- BlockMove(&rect, &(*hTE)->viewRect, sizeof(Rect));
- rect.left += 4; /* indent a bit */
- BlockMove(&rect, &(*hTE)->destRect, sizeof(Rect));
- TECalText(hTE);
- }
-
-